package wpwork.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import wpwork.base.ActionResult;
import wpwork.base.Pagination;
import wpwork.base.UserInfo;
import wpwork.base.controller.SuperController;
import wpwork.base.vo.PageListVO;
import wpwork.base.vo.PaginationVO;
import wpwork.entity.*;
import wpwork.model.ContractForm;
import wpwork.model.ContractInfoVO;
import wpwork.model.ContractListVO;
import wpwork.permission.entity.UserEntity;
import wpwork.service.*;
import wpwork.util.JsonUtil;
import wpwork.util.LoginHolder;
import wpwork.util.ServletUtil;
import wpwork.util.UserProvider;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Contract
 * 版本： V3.0.0
 * 版权： 武汉维普科技有限公司(https://www.websharp.cn)
 * 作者： wpwork开发平台组
 * 日期： 2020-12-31
 * 作业资源
 */
@RestController
@Tag(name = "示例接口", description = "WorkResources")
@RequestMapping("/api/example/WorkResources")
public class WorkResourcesController extends SuperController<WorkResourcesService, WorkResourcesEntity> {

    @Autowired
    private WorkResourcesService workResourcesService;
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private WorkRsattendanceService workRsattendanceService;
    @Autowired
    private WorkRsfenceService workRsfenceService;
    @Autowired
    private WorkRsinspectionService workRsinspectionService;
    @Autowired
    private WorkRsgridService workRsgridService;

    /**
     * 获取列表
     *
     *                   currentPage: 1,
     *         pageSize: 20,
     *         name: "" ,
     *         type: "" ,
     *         attendance: 0 ,
     *         inspection: 0 ,
     *         fence: 0 ,
     *         grid: 0 ,
     * @return
     */
    @Operation(summary = "获取列表")
    @PostMapping("/List")
    public ActionResult<PageListVO<WorkResourcesEntity>> list(@RequestParam int currentPage,
                                                              @RequestParam int pageSize,
                                                              @RequestParam(required = false)  String name,
                                                              @RequestParam(required = false) String  type,
                                                              @RequestParam(required = false) boolean attendance,
                                                              @RequestParam(required = false) boolean inspection,
                                                              @RequestParam(required = false) boolean fence,
                                                              @RequestParam(required = false) boolean  grid) {
        List<WorkResourcesEntity> entity = workResourcesService.getlist(currentPage, pageSize, name, type, attendance, inspection, fence, grid);
        Pagination pagination = new Pagination();
        pagination.setCurrentPage(currentPage);
        pagination.setPageSize(pageSize);
        PaginationVO vo = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
        return ActionResult.page(entity, vo);
    }

    @PostMapping("/getOne")
    public ActionResult<WorkResourcesEntity> getOne(@RequestParam String id) {
        WorkResourcesEntity entity = workResourcesService.getById(id);
        return ActionResult.success(entity);
    }

    @PostMapping("/add")
    public ActionResult<WorkResourcesEntity> add(@RequestBody @Valid WorkResourcesEntity workResources) {
        WorkResourcesEntity entity = new WorkResourcesEntity();
        entity.setId(UUID.randomUUID().toString());
        entity.setLocation(workResources.getLocation() == null ? "[]" : workResources.getLocation());
        entity.setName(workResources.getName());
        entity.setType(workResources.getType());
        entity.setMapShow(workResources.getMapShow());
        entity.setRemark(workResources.getRemark());
        entity.setCreatorTime(new Date());
        String jwtToken = ServletUtil.getRequest().getHeader("Authorization");
        UserInfo userInfo = userProvider.get(jwtToken);
        entity.setLastModifyUserId(userInfo.getUserId());
        boolean save = workResourcesService.save(entity);
        if(save){
            return ActionResult.success("新增成功");
        }else{
            return ActionResult.fail("新增失败");
        }
    }

    @PostMapping("/update")
    public ActionResult<WorkResourcesEntity> update(@RequestBody @Valid WorkResourcesEntity workResources) {
        WorkResourcesEntity entity = new WorkResourcesEntity();
        entity.setId(workResources.getId());
        entity.setLocation(workResources.getLocation());
        entity.setName(workResources.getName());
        entity.setType(workResources.getType());
        entity.setMapShow(workResources.getMapShow());
        entity.setRemark(workResources.getRemark());
        entity.setLastModifyTime(new Date());
        String jwtToken = ServletUtil.getRequest().getHeader("Authorization");
        UserInfo userInfo = userProvider.get(jwtToken);
        entity.setLastModifyUserId(userInfo.getUserId());
        boolean save = workResourcesService.updateById(entity);
        if(save){
            return ActionResult.success("修改成功");
        }else{
            return ActionResult.fail("修改失败");
        }
    }

    @Transactional
    @Operation(summary = "删除")
    @PostMapping("/deleteDataOne")
    public ActionResult<?> deleteDataOne(@RequestParam String id) {
        boolean b = workResourcesService.removeById(id);
        workRsattendanceService.remove(new QueryWrapper<WorkRsattendanceEntity>().eq("f_resources_id", id));
        workRsfenceService.remove(new QueryWrapper<WorkRsfenceEntity>().eq("f_resources_id", id));
        workRsinspectionService.remove(new QueryWrapper<WorkRsinspectionEntity>().eq("f_resources_id", id));
        workRsgridService.remove(new QueryWrapper<WorkRsgridEntity>().eq("f_resources_id", id));
        if (b){
            return ActionResult.success("删除成功");
        }else{
            return ActionResult.fail("删除失败");
        }
    }


}
